package com.yst.gateway.monitor.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yst.gateway.monitor.common.utils.DateUtils;
import com.yst.gateway.monitor.common.utils.LoginUserCache;
import com.yst.gateway.monitor.common.utils.UUIDGenerator;
import com.yst.gateway.monitor.dao.SysMenusMapper;
import com.yst.gateway.monitor.models.SysMenus;
import com.yst.gateway.monitor.services.SysMenuService;

/**
 * 前置系统系统菜单管理业务实现类
 * @author sheungxin
 *
 */
@Service
@Scope("prototype")
public class SysMenuServiceImpl implements SysMenuService{

	@Autowired
	private SysMenusMapper sysMenusMapper;
	
	@Override
	public PageInfo<SysMenus> getByPage(int pageNum, int pageSize,
			Map<String, Object> paramMap) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<SysMenus>(sysMenusMapper.selectAll(paramMap));
	}

	@Override
	public boolean add(SysMenus t) {
		boolean flag=true;
		t.setSysMenuId(UUIDGenerator.getUUID());
		t.setSysMenuCreator(LoginUserCache.getUserId());
		t.setSysMenuCtime(DateUtils.getNowTime());
		t.setSysMenuStatus(1);
		t.setSysMenuDel(1);
		t.setSysMenuLevel(getLevelByPid(t.getSysMenuPid()));
		t.setSysMenuSort(getSortByLevel(t.getSysMenuLevel()));
		if(t.getSysMenuPid()==null){
			t.setSysMenuPid("0");
		}
		if(sysMenusMapper.insert(t)>0){
			
		}else{
			flag=false;
		}
		return flag;
	}
	
	@Override
	public boolean update(SysMenus t) {
		boolean flag=true;
		if(t.getSysMenuCtime()!=null){
			t.setSysMenuCtime(null);
		}
		if(t.getSysMenuCreator()!=null){
			t.setSysMenuCreator(null);
		}
		if(t.getSysMenuStatus()!=null){
			t.setSysMenuStatus(null);
		}
		if(t.getSysMenuDel()!=null){
			t.setSysMenuDel(null);
		}
		if(StringUtils.isBlank(t.getSysMenuPid())){
			t.setSysMenuPid("0");
		}
		t.setSysMenuLevel(getLevelByPid(t.getSysMenuPid()));
		t.setSysMenuModifier(LoginUserCache.getUserId());
		t.setSysMenuUtime(DateUtils.getNowTime());
		if(sysMenusMapper.updateByPrimaryKey(t)>0){
			flag=true;
		}else{
			flag=false;
		}
		return flag;
	}

	@Override
	public SysMenus getById(String id) {
		return sysMenusMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<SysMenus> getByMap(Map<String, Object> paramMap) {
		return sysMenusMapper.selectAll(paramMap);
	}

	@Override
	public boolean updateStatusById(String id) {
		boolean flag=false;
		SysMenus t=getById(id);
		if(t!=null){
			SysMenus upMenu=new SysMenus();
			upMenu.setSysMenuId(id);
			upMenu.setSysMenuStatus(t.getSysMenuStatus()==0?1:0);
			upMenu.setSysMenuUtime(DateUtils.getNowTime());
			upMenu.setSysMenuModifier(LoginUserCache.getUserId());
			flag=sysMenusMapper.updateByPrimaryKey(upMenu)>0?true:false;
		}
		return flag;
	}

	@Override
	public boolean delById(String id) {
		SysMenus upMenu=new SysMenus();
		upMenu.setSysMenuId(id);
		upMenu.setSysMenuDel(0);
		upMenu.setSysMenuUtime(DateUtils.getNowTime());
		upMenu.setSysMenuModifier(LoginUserCache.getUserId());
		return sysMenusMapper.updateByPrimaryKey(upMenu)>0?true:false;
	}
	
	@Override
	public List<SysMenus> getTreeByMap(Map<String, Object> paramMap) {
		List<SysMenus> menuList=sysMenusMapper.selectAll(paramMap);
		List<SysMenus> menuTreeList=new ArrayList<SysMenus>();
		Map<String, SysMenus> menuMap=new HashMap<String, SysMenus>();
		for(SysMenus menu:menuList){
			menuMap.put(menu.getSysMenuId(), menu);
			if(menu.getSysMenuPid().equals("0")){
				menuTreeList.add(menu);
			}else{
				if(menuMap.get(menu.getSysMenuPid())!=null){
					menuMap.get(menu.getSysMenuPid()).getChildList().add(menu);
				}
			}
		}
		return menuTreeList;
	}

	/**
	 * 获取当前级别菜单的最大排序编号
	 * @param level
	 * @return
	 */
	private int getSortByLevel(Integer level){
		Integer maxSort=0;
		Map<String, Object> map=sysMenusMapper.selectMaxSortByLevel(level);
		if(map!=null&&map.get("max_sort")!=null){
			maxSort=(Integer)map.get("max_sort")+1;
		}
		return maxSort;
	}
	
	/**
	 * 通过上级菜单编号，获取下级菜单的级别
	 * @param pid
	 * @return
	 */
	private int getLevelByPid(String pid){
		int level=0;
		if(StringUtils.isNotBlank(pid)){
			SysMenus pMenu=getById(pid);
			if(pMenu!=null){
				level=pMenu.getSysMenuLevel()+1;
			}
		}
		return level;
	}

}
